// pages/gaia-demo/gaia-demo.js
import gaiaManager from '../../utils/gaiaManager.js';

Page({
  data: {
    isConnected: false,
    deviceInfo: null,
    volume: 0,
    muted: false,
    eqEnabled: false,
    ancEnabled: false,
    transparencyEnabled: false,
    loading: false,
    logs: []
  },

  onLoad() {
    this.addLog('页面加载完成');
  },

  // 添加日志
  addLog(message) {
    const logs = this.data.logs;
    logs.unshift({
      time: new Date().toLocaleTimeString(),
      message: message
    });
    if (logs.length > 20) logs.pop(); // 只保留最近20条日志
    this.setData({ logs });
  },

  // 连接设备
  async connectDevice() {
    this.setData({ loading: true });
    this.addLog('正在连接设备...');
    
    try {
      const result = await gaiaManager.connect();
      if (result.success) {
        this.setData({ isConnected: true });
        this.addLog('设备连接成功');
        this.loadDeviceInfo();
      } else {
        this.addLog(`连接失败: ${result.error}`);
      }
    } catch (error) {
      this.addLog(`连接错误: ${error.message}`);
    } finally {
      this.setData({ loading: false });
    }
  },

  // 断开连接
  disconnectDevice() {
    gaiaManager.disconnect();
    this.setData({ 
      isConnected: false,
      deviceInfo: null,
      volume: 0,
      muted: false,
      eqEnabled: false,
      ancEnabled: false,
      transparencyEnabled: false
    });
    this.addLog('设备已断开连接');
    
    // 返回发现页面
    wx.navigateBack({
      delta: 1
    });
  },

  // 加载设备信息
  async loadDeviceInfo() {
    try {
      const deviceInfo = await gaiaManager.getDeviceInfo();
      this.setData({ deviceInfo });
      this.addLog('设备信息加载完成');
      
      // 加载音频状态
      await this.loadAudioState();
    } catch (error) {
      this.addLog(`加载设备信息失败: ${error.message}`);
    }
  },

  // 加载音频状态
  async loadAudioState() {
    try {
      // 获取音量
      const volumeResult = await gaiaManager.getVolume();
      this.setData({ volume: volumeResult.volume });
      
      // 获取静音状态
      const muteResult = await gaiaManager.getMute();
      this.setData({ muted: muteResult.muted });
      
      // 获取均衡器状态
      const eqSupport = await gaiaManager.getEqualizerSupport();
      if (eqSupport.supported) {
        const eqState = await gaiaManager.getEqualizerState();
        this.setData({ eqEnabled: eqState.enabled });
      }
      
      // 获取降噪状态
      const ancSupport = await gaiaManager.getANCSupport();
      if (ancSupport.supported) {
        const ancState = await gaiaManager.getANCState();
        this.setData({ ancEnabled: ancState.enabled });
      }
      
      // 获取透明模式状态
      const transparencySupport = await gaiaManager.getTransparencySupport();
      if (transparencySupport.supported) {
        const transparencyState = await gaiaManager.getTransparencyState();
        this.setData({ transparencyEnabled: transparencyState.enabled });
      }
      
      this.addLog('音频状态加载完成');
    } catch (error) {
      this.addLog(`加载音频状态失败: ${error.message}`);
    }
  },

  // 设置音量
  async setVolume(e) {
    const volume = e.detail.value;
    this.setData({ volume });
    
    try {
      const result = await gaiaManager.setVolume(volume);
      if (result.success) {
        this.addLog(`音量设置为: ${volume}`);
      } else {
        this.addLog(`设置音量失败: ${result.error}`);
      }
    } catch (error) {
      this.addLog(`设置音量错误: ${error.message}`);
    }
  },

  // 切换静音
  async toggleMute() {
    const newMuted = !this.data.muted;
    this.setData({ muted: newMuted });
    
    try {
      const result = await gaiaManager.setMute(newMuted);
      if (result.success) {
        this.addLog(newMuted ? '已静音' : '已取消静音');
      } else {
        this.addLog(`设置静音失败: ${result.error}`);
      }
    } catch (error) {
      this.addLog(`设置静音错误: ${error.message}`);
    }
  },

  // 切换均衡器
  async toggleEqualizer() {
    const newEqEnabled = !this.data.eqEnabled;
    this.setData({ eqEnabled: newEqEnabled });
    
    try {
      const result = await gaiaManager.setEqualizerState(newEqEnabled);
      if (result.success) {
        this.addLog(newEqEnabled ? '均衡器已启用' : '均衡器已禁用');
      } else {
        this.addLog(`设置均衡器失败: ${result.error}`);
      }
    } catch (error) {
      this.addLog(`设置均衡器错误: ${error.message}`);
    }
  },

  // 切换降噪
  async toggleANC() {
    const newAncEnabled = !this.data.ancEnabled;
    this.setData({ ancEnabled: newAncEnabled });
    
    try {
      const result = await gaiaManager.setANCState(newAncEnabled);
      if (result.success) {
        this.addLog(newAncEnabled ? '降噪已启用' : '降噪已禁用');
      } else {
        this.addLog(`设置降噪失败: ${result.error}`);
      }
    } catch (error) {
      this.addLog(`设置降噪错误: ${error.message}`);
    }
  },

  // 切换透明模式
  async toggleTransparency() {
    const newTransparencyEnabled = !this.data.transparencyEnabled;
    this.setData({ transparencyEnabled: newTransparencyEnabled });
    
    try {
      const result = await gaiaManager.setTransparencyState(newTransparencyEnabled);
      if (result.success) {
        this.addLog(newTransparencyEnabled ? '透明模式已启用' : '透明模式已禁用');
      } else {
        this.addLog(`设置透明模式失败: ${result.error}`);
      }
    } catch (error) {
      this.addLog(`设置透明模式错误: ${error.message}`);
    }
  },

  // 清空日志
  clearLogs() {
    this.setData({ logs: [] });
  },

  // 返回上一页
  goBack() {
    if (this.data.isConnected) {
      wx.showModal({
        title: '提示',
        content: '设备已连接，是否断开连接并返回？',
        success: (res) => {
          if (res.confirm) {
            this.disconnectDevice();
          }
        }
      });
    } else {
      wx.navigateBack({
        delta: 1
      });
    }
  }
});
